package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.MemberEventMapper;
import com.rainng.coursesystem.model.entity.MemberEventEntity;
import com.rainng.coursesystem.model.vo.response.table.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class MemberEventDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;
    private final MemberEventMapper mapper;

    public MemberEventDAO(MemberEventMapper mapper) {
        this.mapper = mapper;
    }


    public int insert(MemberEventEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public MemberEventEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(MemberEventEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String teamName, String eventName, String memberName) {
        return mapper.count(teamName, eventName, memberName);
    }

    public List<MemberEventItemVO> getPage(Integer index, String teamName, String eventName, String memberName) {
        Page<MemberEventItemVO> page = new Page<>(index, PAGE_SIZE);

        return mapper.getPage(page, teamName, eventName, memberName).getRecords();
    }

    public int countByEventId(Integer eventId) {
        LambdaQueryWrapper<MemberEventEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEventEntity::getEventId, eventId);

        return mapper.selectCount(wrapper);
    }

    public int countByMemberId(Integer memberId) {
        LambdaQueryWrapper<MemberEventEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberEventEntity::getMemberId, memberId);

        return mapper.selectCount(wrapper);
    }

    public MemberEventEntity getByEventIdAndMemberId(Integer eventId, Integer memberId) {
        LambdaQueryWrapper<MemberEventEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(MemberEventEntity::getId)
                .eq(MemberEventEntity::getEventId, eventId)
                .eq(MemberEventEntity::getMemberId, memberId);

        return mapper.selectOne(wrapper);
    }

    public List<MemberEventSelectedItemVO> listMemberEventSelected(Integer memberId) {
        return mapper.listMemberEventSelected(memberId);
    }

    public Integer countMemberEventSelectedByTimePart(Integer memberId, String timePart) {
        return mapper.countMemberEventSelectedByTimePart(memberId, timePart);
    }

    public List<TimetableItemVO> listMemberTimetable(Integer memberId) {
        return mapper.listMemberTimetable(memberId);
    }

    public Integer countLeaderBudget(Integer leaderId, String eventName, String memberName) {
        return mapper.countLeaderBudget(leaderId, eventName, memberName);
    }

    public List<LeaderBudgetItemVO> getLeaderBudgetPage(Integer index, Integer leaderId, String eventName, String memberName) {
        Page<LeaderBudgetItemVO> page = new Page<>(index, PAGE_SIZE);
        return mapper.getLeaderBudgetPage(page, leaderId, eventName, memberName).getRecords();
    }
}
